SWEP.Author			= "Dacre Denny"
SWEP.Contact			= "dacredens@gmail.com"
SWEP.Purpose			= "Enable core functions in Producer"
SWEP.Instructions		= "Left/Right click"

SWEP.ViewModel			= "models/weapons/v_toolgun.mdl"
SWEP.WorldModel			= "models/weapons/w_toolgun.mdl"
SWEP.AnimPrefix			= "python"

util.PrecacheModel( SWEP.ViewModel )
util.PrecacheModel( SWEP.WorldModel )

SWEP.ShootSound			= Sound( "Airboat.FireGunRevDown" )

SWEP.Tool				= {}

include('mode_camera.lua')
include('mode_interact.lua')
include('mode_actsequence.lua')


SWEP.Primary = 
{
	ClipSize 	= -1,
	DefaultClip = -1,
	Automatic = false,
	Ammo = "none"
}

SWEP.Secondary = 
{
	ClipSize 	= -1,
	DefaultClip = -1,
	Automatic = false,
	Ammo = "none"
}

SWEP.CanHolster			= true
SWEP.CanDeploy			= true

STATE_INTERACT = "Interact"
STATE_CREATE_ANIMATION_CAMERA = "CreateAniCam"
STATE_CREATE_STILL_CAMERA = "CreateStillCam"
STATE_CREATE_ACTOR_SEQUENCE = "CreateActorSequence"
STATE_GATHER_SEQUENCE_ACTORS = "GatherSequenceActors"
STATE_GATHER_CAMERA_SEQUENCES = "GatherCamereaSequences"

function SWEP:Initialize()
	
	self.State = STATE_INTERACT
	self.Primary = 
	{
		ClipSize 	= -1,
		DefaultClip = -1,
		Automatic = false,
		Ammo = "none"
	}
	
	self.Secondary = 
	{
		ClipSize 	= -1,
		DefaultClip = -1,
		Automatic = false,
		Ammo = "none"
	}
	
end

function SWEP:OnRestore()
	
end

function SWEP:Precache()

	util.PrecacheSound( self.ShootSound )
	
end

function SWEP:Reload()
	
end

//****************************************************
//* Enables a name to be assigned to this track. Only
//* the root node of this track is assigned data
function SWEP:SetTrackName( name )
	if self:GetState() == STATE_CREATE_ANIMATION_CAMERA then
		MODE_ANICAMERA.CurrentName = name
	elseif self:GetState() == STATE_CREATE_STILL_CAMERA then
		MODE_STATIC_CAMERA.CurrentName = name
	elseif self:GetState() == STATE_GATHER_SEQUENCE_ACTORS or self:GetState() == STATE_CREATE_ACTOR_SEQUENCE then
		MODE_ACTORSEQUENCE.CurrentName = name
	end
end

//****************************************************
//* Enables the state of the tool to be set externally.
//* This causes all variables specfic to certian tool
//* states to be reset if the state of the tool is actually
//* changing
function SWEP:SetState( state ) 

	if self.State != STATE_CREATE_ANIMATION_CAMERA then
		MODE_ANICAMERA.PrevCamera = nil
	end
	if self.State != STATE_CREATE_ACTOR_SEQUENCE then
		MODE_ACTORSEQUENCE.PrevNode = nil
	end
	if self.State != STATE_GATHER_SEQUENCE_ACTORS then
		MODE_SEQUENCEGATHERACTORS.TargetWaypoint = nil
		MODE_SEQUENCEGATHERACTORS.LastMouseOverNPC = nil
		MODE_SEQUENCEGATHERACTORS.GlowEntity = nil
	end

	if self.State != STATE_GATHER_CAMERA_SEQUENCES then
		MODE_CAMERAGATHERSEQUENCE.TargetCamera = nil
	end
	self.State = state
	self.Owner:SetNWString("InteractState", state)

end

function SWEP:GetState( ) 

	return self.Owner:GetNWString("InteractState")
end
function SWEP:GetMode()
	
	return self.Mode
	
end

function SWEP:Think()
	if SERVER then
		if self.State == STATE_INTERACT then
			MODE_INTERACT:Think(self.Owner, util.TraceLine( util.GetPlayerTrace( self.Owner )))
		elseif self.State == STATE_CREATE_ACTOR_SEQUENCE then
			MODE_ACTORSEQUENCE:Think(self.Owner, util.TraceLine( util.GetPlayerTrace( self.Owner )))
		elseif self.State == STATE_CREATE_ANIMATION_CAMERA then
			MODE_ANICAMERA:Think(self.Owner, util.TraceLine( util.GetPlayerTrace( self.Owner )))
		elseif self.State == STATE_CREATE_STILL_CAMERA  then
			MODE_STATIC_CAMERA:Think(self.Owner, util.TraceLine( util.GetPlayerTrace( self.Owner )))
		elseif self:GetState() == STATE_GATHER_SEQUENCE_ACTORS  then
			MODE_SEQUENCEGATHERACTORS:Think(self.Owner, util.TraceLine( util.GetPlayerTrace( self.Owner )))
		elseif self:GetState() == STATE_GATHER_CAMERA_SEQUENCES  then
			MODE_CAMERAGATHERSEQUENCE:Think(self.Owner, util.TraceLine( util.GetPlayerTrace( self.Owner )))
		end
	end

	if CLIENT then		
		if self:GetState() == STATE_GATHER_SEQUENCE_ACTORS  then
			MODE_SEQUENCEGATHERACTORS:Think(LocalPlayer(), util.TraceLine( util.GetPlayerTrace( self.Owner )))
		end
	end
end

function SWEP:DoShootEffect( hitpos, hitnormal, entity, physbone )
	self.Weapon:SendWeaponAnim( ACT_VM_PRIMARYATTACK )
end

function SWEP:PrimaryAttack()

	if SERVER then
		if self.State == STATE_INTERACT then
			MODE_INTERACT:LeftClick(self.Owner,  self.Owner:GetEyeTrace())
		elseif self.State == STATE_CREATE_ACTOR_SEQUENCE then
			MODE_ACTORSEQUENCE:LeftClick(self.Owner, util.TraceLine( util.GetPlayerTrace( self.Owner )))
		elseif self.State == STATE_CREATE_ANIMATION_CAMERA then
			MODE_ANICAMERA:LeftClick(self.Owner,  self.Owner:GetEyeTrace())
		elseif self.State == STATE_CREATE_STILL_CAMERA  then
			MODE_STATIC_CAMERA:LeftClick(self.Owner,  self.Owner:GetEyeTrace())
		elseif self:GetState() == STATE_GATHER_SEQUENCE_ACTORS  then
			MODE_SEQUENCEGATHERACTORS:LeftClick(self.Owner, util.TraceLine( util.GetPlayerTrace( self.Owner )))
		elseif self:GetState() == STATE_GATHER_CAMERA_SEQUENCES  then
			MODE_CAMERAGATHERSEQUENCE:LeftClick(self.Owner, util.TraceLine( util.GetPlayerTrace( self.Owner )))
		end
	end	

end


/*---------------------------------------------------------
	SecondaryAttack - Reset everything to how it was
---------------------------------------------------------*/
function SWEP:SecondaryAttack()
	
	if SERVER then
		if self.State == STATE_INTERACT then
			MODE_INTERACT:RightClick(self.Owner,  self.Owner:GetEyeTrace())
		elseif self.State == STATE_CREATE_ACTOR_SEQUENCE then
			MODE_ACTORSEQUENCE:RightClick(self.Owner, util.TraceLine( util.GetPlayerTrace( self.Owner )))
		elseif self.State == STATE_CREATE_ANIMATION_CAMERA then
			MODE_ANICAMERA:RightClick(self.Owner,  self.Owner:GetEyeTrace())
		elseif self.State == STATE_CREATE_STILL_CAMERA  then
			MODE_STATIC_CAMERA:RightClick(self.Owner,  self.Owner:GetEyeTrace())
		elseif self:GetState() == STATE_GATHER_SEQUENCE_ACTORS  then
			MODE_SEQUENCEGATHERACTORS:RightClick(self.Owner, util.TraceLine( util.GetPlayerTrace( self.Owner )))
		elseif self:GetState() == STATE_GATHER_CAMERA_SEQUENCES  then
			MODE_CAMERAGATHERSEQUENCE:RightClick(self.Owner, util.TraceLine( util.GetPlayerTrace( self.Owner )))
		end
	end	
	
	if CLIENT then		
		if self:GetState() == STATE_GATHER_SEQUENCE_ACTORS  then
			MODE_SEQUENCEGATHERACTORS:RightClick(LocalPlayer(), util.TraceLine( util.GetPlayerTrace( self.Owner )))
		end
	end
end

/*---------------------------------------------------------
   Name: ContextScreenClick(  aimvec, mousecode, pressed, ply )
---------------------------------------------------------*/
function SWEP:ContextScreenClick( aimvec, mousecode, pressed, ply )

	if ( !pressed ) then return end
	
	// No prediction in single player
	if (CLIENT && SinglePlayer()) then return end 
	
	local ViewEnt = ply
	
	if (CLIENT) then ViewEnt = GetViewEntity() end
	if (SERVER) then ViewEnt = ply:GetViewEntity() end
	
	local mode = self:GetMode()
	local tr = utilx.GetPlayerTrace( ViewEnt, aimvec )
	local trace = util.TraceLine( tr )
	if ( !trace.Hit ) then return end
	
	local tool = self:GetToolObject()
	if ( !tool ) then return end
	
	tool:CheckObjects()
	
	// Ask the gamemode if it's ok to do this
	if ( !tool:Allowed() ) then return end
	if ( !gamemode.Call( "CanTool", self.Owner, trace, mode ) ) then return end
	
	if ( mousecode == MOUSE_LEFT ) then
		if ( !tool:LeftClick( trace ) ) then return end
	else
		if ( !tool:RightClick( trace ) ) then return end
	end
	
	self:DoShootEffect( trace.HitPos, trace.HitNormal, trace.Entity, trace.PhysicsBone )
	
end

/*---------------------------------------------------------
	Holster
---------------------------------------------------------*/
function SWEP:Holster()

	// Just do what the SWEP wants to do if there's no tool
	if ( !self:GetToolObject() ) then return self.CanHolster end
	
	local CanHolster = self:GetToolObject():Holster()
	if ( CanHolster ~= nil ) then return CanHolster end
	
	return self.CanHolster
	
end

/*---------------------------------------------------------
	OnRemove
	- Delete ghosts here in case the weapon gets deleted all of a sudden somehow
---------------------------------------------------------*/
function SWEP:OnRemove()

	if ( !self:GetToolObject() ) then return end
	
	self:GetToolObject():ReleaseGhostEntity()
	
end


/*---------------------------------------------------------
	OwnerChanged
	- This will remove any ghosts when a player dies and drops the weapon
---------------------------------------------------------*/
function SWEP:OwnerChanged()

	if ( !self:GetToolObject() ) then return end
	
	self:GetToolObject():ReleaseGhostEntity()
	
end

/*---------------------------------------------------------
	Deploy
---------------------------------------------------------*/
function SWEP:Deploy()

	// Just do what the SWEP wants to do if there is no tool
	if ( !self:GetToolObject() ) then return self.CanDeploy end
	
	self:GetToolObject():UpdateData()
	
	local CanDeploy = self:GetToolObject():Deploy()
	if ( CanDeploy ~= nil ) then return CanDeploy end
	
	return self.CanDeploy
	
end

function SWEP:GetToolObject( tool )
	
	local mode = tool or self:GetMode()
	
	if ( !self.Tool[ mode ] ) then return false end
	
	return self.Tool[ mode ]
	
end
